<template>
  <el-dialog :visible="showAssignDialog" title="分配权限" @close="hClose">
    <el-tree
      ref="tree"
      node-key="id"
      check-strictly
      default-expand-all
      :data="permissionList"
      :props="{label:'name'}"
      show-checkbox
    >
      <template v-slot="{ data }">
        <el-row>
          <el-col>{{ data.name }}</el-col>
        </el-row>
      </template>
    </el-tree>
    <el-button size="mini" type="primary" @click="doSave">保存</el-button>
  </el-dialog>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { getRolesDetail, assignPermission } from '@/api/roles'
import { transListToTreeData } from '@/utils/index'
export default {
  name: 'AssionPermission',
  props: {
    showAssignDialog: {
      type: Boolean,
      required: true
    },
    currentId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      permissionList: []
    }
  },
  async created() {
    await this.loadPermission()
  },
  methods: {
    hClose() {
      this.$emit('update:showAssignDialog', false)
    },
    async loadPermission() {
      const res = await getPermissionList()
      console.log(res.data)
      this.permissionList = transListToTreeData(res.data, 0)
    },
    async getPermIds() {
      const res = await getRolesDetail(this.currentId)
      console.log(res)
      this.$refs.tree.setCheckedKeys(res.data.permIds, true)
    },
    async doSave() {
      const keys = this.$refs.tree.getCheckedKeys()
      console.log(this.currentId, keys)
      const res = await assignPermission({ id: this.currentId, permIds: keys })
      console.log(res)
      this.$message.success(res.message)
      this.hClose()
    }
  }
}
</script>

<style>

</style>
